﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    class ObjectManager
    {
        public List<BaseObject> m_ObjectList = new List< BaseObject>();
        private static ObjectManager m_Instance = new ObjectManager();

        public static ObjectManager Instance
        {
            get
            {
                return m_Instance;
            }
        }

        public ObjectManager()
        {

        }

        public void Update(float deltaT, KeyboardState LastKeyState)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Q) && LastKeyState.IsKeyUp(Keys.Q))
            {
                Dialogue textBox = new Dialogue();
                textBox.m_szText = "Lorem Ipsum sub arbore sedent, something about a key.\nName of town <blah blah blah> NPC text etcetera....\n whatever.";
                ObjectManager.Instance.m_ObjectList.Add(textBox);
            }

            m_ObjectList.Sort(delegate(BaseObject bo1, BaseObject bo2) { return bo1.m_fDepth.CompareTo(bo2.m_fDepth); });

            for (int i=0; i<m_ObjectList.Count; i++) 
            {
                m_ObjectList[i].Update(deltaT, LastKeyState);
                if (m_ObjectList[i].m_ObjectType == BaseObject.OBJ_TYPE.OBJ_PARTICLE || m_ObjectList[i].m_ObjectType == BaseObject.OBJ_TYPE.OBJ_TILE)
                    continue;
                for (int p = 0; p < m_ObjectList.Count; p++)
                {
                    if (m_ObjectList[p].m_ObjectType == BaseObject.OBJ_TYPE.OBJ_PARTICLE)
                        continue;
                    if (m_ObjectList[p] != m_ObjectList[i])
                    {
                        m_ObjectList[i].CollisionResponse(m_ObjectList[p]);
                        m_ObjectList[p].CollisionResponse(m_ObjectList[i]);
                    }
                }
            }

            for (int i=0; i<m_ObjectList.Count;)
            {
                if (m_ObjectList[i].m_bIsAlive == false)
                    m_ObjectList.Remove(m_ObjectList[i]);
                else
                    i++;

            }
        }

        public void Render(SpriteBatch spriteBatch, Camera cam)
        {
            foreach (BaseObject obj in m_ObjectList)
            {
                if (obj.m_bAdditive == false)
                    obj.Render(spriteBatch, cam);
            }
            spriteBatch.End();
            spriteBatch.Begin(SpriteBlendMode.Additive);
            foreach (BaseObject obj in m_ObjectList)
            {
                if (obj.m_bAdditive == true)
                    obj.Render(spriteBatch, cam);
            }
            spriteBatch.End();
            spriteBatch.Begin();
        }

        public Player GetPlayer() {

            foreach (BaseObject obj in m_ObjectList)
            {
                if (obj.m_ObjectType == BaseObject.OBJ_TYPE.OBJ_PLAYER)
                {
                    return (Player)obj;
                }
            }

            return null;
        }

    }
}
